package com.coderman.common.starter.nacos.datadict.data;

import java.util.HashMap;
import java.util.Map;

/**
 * 自定义map
 * @param <V>  用一个name表示这个map存的是什么数据
 */
public class StrictMap <V> extends HashMap<String, V> {
    private static final long serialVersionUID = -4950446264854982944L;
    private final String name;

    public StrictMap(String name, int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor);
        this.name = name;
    }

    public StrictMap(String name, int initialCapacity) {
        super(initialCapacity);
        this.name = name;
    }

    public StrictMap(String name) {
        super();
        this.name = name;
    }

    public StrictMap(String name, Map<String, ? extends V> m) {
        super(m);
        this.name = name;
    }

    @SuppressWarnings("unchecked")
    @Override
    public V put(String key, V value) {
        if (containsKey(key)) {
            throw new IllegalArgumentException(name + " already contains value for " + key);
        }

        return super.put(key, value);
    }

    @Override
    public V get(Object key) {
        V value = super.get(key);
        if (value == null) {
            //throw new IllegalArgumentException(name + " does not contain value for " + key);
            return null;
        }
        return value;
    }

    @Override
    public String toString(){
       return "";
    }

}
